package com.demo.dao.impl;

import com.demo.dao.ShangPingDao;
import com.demo.domain.Customer;
import com.demo.domain.Shangping;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;

import java.util.List;

/**
 * @Version 2019
 * @Author:kele
 * @Date:2020/4/19
 * @Content:
 */
public class ShangPingDaoImpl extends HibernateDaoSupport implements ShangPingDao {
    @Override
    public void saveShangping(Shangping shangping) {
        getHibernateTemplate().save(shangping);
    }

    @Override
    public List<Shangping> find() {
        List<Shangping> list = (List<Shangping>) getHibernateTemplate().
                find("from Shangping");
        return list;
    }

    @Override
    public Shangping findOne(int s_id) {
        return getHibernateTemplate().get(Shangping.class, s_id);
    }

    @Override
    public void delete(Shangping s) {
        getHibernateTemplate().delete(s);
    }

    @Override
    public void update(Shangping shangping) {
        getHibernateTemplate().update(shangping);
    }

    @Override
    public List<Shangping> findMoreCondition(Shangping shangping) {
        // TODO Auto-generated method stub
        // 创建离线对象，指定对哪个实体类进行操作
        DetachedCriteria criteria = DetachedCriteria.forClass(Shangping.class);
        // 判断条件值是否为空
        if (shangping.getS_name() != null
                && !"".equals(shangping.getS_name())) {
            // 设置对属性，设置值
            criteria.add(Restrictions.eq("s_name", shangping.getS_name()));
        }
        // if (customer.getCustLevel() != null
        // && !"".equals(customer.getCustLevel())) {
        // criteria.add(Restrictions.eq("custLevel", customer.getCustLevel()));
        // }
        if (shangping.getS_price() != null
                && !"".equals(shangping.getS_price())) {
            criteria.add(Restrictions.eq("s_price",
                    shangping.getS_price()));
        }

        return (List<Shangping>) this.getHibernateTemplate().findByCriteria(
                criteria);
    }
}
